package com.thanh.icook;
import java.util.ArrayList;
import java.util.Random;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class ThucdonActivity extends Activity {
	private String[] Thu = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
	ArrayList<CNO> _CNO = new ArrayList<CNO>();
	GridView gV_ListdishWeek;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_thucdon);
        settab();
        gV_ListdishWeek = (GridView) findViewById(R.id.gridView1);
        setdishforweek();
    }
	
private void  setdishforweek() {
	choosenDish(_CNO, Thu);
	ArrayList<String> listDish_Sang = new ArrayList<String>();
	ArrayList<String> listDish_Trua = new ArrayList<String>();
	ArrayList<String> listDish_Chieu = new ArrayList<String>();
	ArrayList<String> listNo = new ArrayList<String>();
	GridView gV_ListdishWeek = (GridView) findViewById(R.id.gridView1);
	for (CNO _d : _CNO) {
		ArrayList<Dish> listDish = _d.getDish();
		String _sang = "";
		String _trua = "";
		String _chieu = "";
		for (Dish dish : listDish) {
			if(dish.getType().equals("S%C3%A1ng"))
				_sang += dish.getTitle() + "\n";
			if(dish.getType().equals("Tr%C6%B0a"))
				_trua += dish.getTitle() + "\n";
			if(dish.getType().equals("Chi%E1%BB%81u"))
				_chieu += dish.getTitle() + "\n";
		}
		listDish_Sang.add(_sang);
		listDish_Trua.add(_trua);
		listDish_Chieu.add(_chieu);
		listNo.add(_d.getTitle());
	}
	GridviewAdapter_Week mAdapter = 
		new GridviewAdapter_Week(this, listNo, listDish_Sang, listDish_Trua, listDish_Chieu);
	gV_ListdishWeek.setAdapter(mAdapter);
}	
	
private void choosenDish(ArrayList<CNO> _n, String[] _NO) {
	for(int i = 0; i<_NO.length;i++){
		CNO _cno = new CNO(); //lay mon an cho tung ngay va luu vao bien nay
		ArrayList<Dish> _dish = new ArrayList<Dish>(); //Danh sach chua thuc an trong NO
		// TODO Lay danh sach thuc an: Sang 1, Trua 4, Chieu 3, Khuya 1
		laythucan(_dish, "S%C3%A1ng", "Canh", 1);
		laythucan(_dish, "S%C3%A1ng", "C%C6%A1m", 1);
		laythucan(_dish, "Tr%C6%B0a", "Canh", 1);
		laythucan(_dish, "Tr%C6%B0a", "Kho", 1);
		laythucan(_dish, "Tr%C6%B0a", "X%C3%A0o", 1);
		laythucan(_dish, "Tr%C6%B0a", "N%E1%BA%A5u", 1);
		laythucan(_dish, "Chi%E1%BB%81u", "N%C6%B0%E1%BB%9Bng", 1);
		laythucan(_dish, "Chi%E1%BB%81u", null, 1);
		_cno.setDish(_dish); // trong _cno phai chua danh sach thuc an
		_cno.setID(i);
		_cno.setTitle(_NO[i]);
		_n.add(_cno); //Them doi tuong CNO vao danh sach
	}
}	
private void laythucan(ArrayList<Dish> dish, String type, String method, int slg) {
	Creadfile read = new Creadfile();
	String str;
	if(method==null) str = read.readJsonURL("http://icook.dinhchithanh.com/getdishwhere.php?type="+type);
	else str = read.readJsonURL("http://icook.dinhchithanh.com/getdishwhere.php?type="+type+"&method="+method);
	Random ran = new Random();
	try {	
		//Chon mon an
		JSONArray entries = new JSONArray(str);
		for (int i = 0; i < slg; i++) {
			int id = ran.nextInt(entries.length());
	 		JSONObject post = entries.getJSONObject(id);
	 		Dish _dish = new Dish();
	 		_dish.setId(post.getInt("id"));
	 		_dish.setTitle(post.getString("title"));
	 		_dish.setDescription(post.getString("description"));
	 		_dish.setEnergy(post.getInt("energy"));
	 		_dish.setType(type);
	 		dish.add(_dish);
		}
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
}	
private void settab() {
		//Khoi tao Tabhost
        TabHost host = (TabHost) findViewById(R.id.tabhost_thucdon);
        host.setup();
        //Tab mon an
        TabSpec allScoresTab = host.newTabSpec("tabweek");
        allScoresTab.setIndicator(getResources().getString(R.string.tab_week), getResources().getDrawable(
        				R.drawable.week));
        allScoresTab.setContent(R.id.tab1);
        //getCateDish();     
        host.addTab(allScoresTab);
        //Tab nguoi an
        TabSpec friendScoresTab = host.newTabSpec("tabmonth");
        friendScoresTab.setIndicator(getResources().getString(R.string.tab_month), 
        		getResources().getDrawable(R.drawable.month));
        friendScoresTab.setContent(R.id.tab2);  
        //getPeople();
        host.addTab(friendScoresTab);
        //Chon tab lam mac dinh
        host.setCurrentTabByTag("tabweek");
        
	}
}
